package com.itholmes.tomcat;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.itholmes.ServletConfig.ServletConfig;
import com.itholmes.ServletContext.ServletContext;

public class MyRequest {
	
	//请求方法
	private String request_method;
	
	//请求url
	private String request_url;
	
	//请求http版本
	private String request_http_version;
	
	//setattribute和getattribute的容器
	private Map<String,Object> attribute = new HashMap<String,Object>();
	
	//请求头的信息
	private Map<String,String> request_head = new HashMap<String,String>();
	
	//参数信息
	private Map<String,String> paramter = new HashMap<String,String>();
	
	//servletContext对象
	private ServletContext context;
	
	//servletConfig对象
	private ServletConfig config;
	
	public ServletConfig getConfig() {
		return config;
	}

	public void setConfig(ServletConfig config) {
		this.config = config;
	}

	public ServletContext getContext() {
		return context;
	}

	public void setContext(ServletContext context) {
		this.context = context;
	}

	public void setAttribute(String key,Object value) {
		attribute.put(key, value);
	}
	
	public Object getAttribute(String key) {
		return attribute.get(key);
	}
	
	public String getRequest_method() {
		return request_method;
	}

	public void setRequest_method(String request_method) {
		this.request_method = request_method;
	}

	public String getRequest_url() {
		return request_url;
	}

	public void setRequest_url(String request_url) {
		this.request_url = request_url;
	}

	public String getRequest_http_version() {
		return request_http_version;
	}

	public void setRequest_http_version(String request_http_version) {
		this.request_http_version = request_http_version;
	}


	public Map<String, String> getParamter() {
		return paramter;
	}

	public void setParamter(Map<String, String> paramter) {
		this.paramter = paramter;
	}

	public ServletContext getServletContext() {
		return context;
	}
	
	//拿到http协议初始化这些方法
	public MyRequest(InputStream in) throws IOException {
		
		//解析输入流
		int available = in.available();
		byte[] b = new byte[available];
		in.read(b);
		
		//获得http请求
		String http_str = new String(b);
		String[] split = http_str.split("\r\n");
		
		//获取method，url，http版本,\\s是匹配任何空白字符，包括空格、制表符、换页符等等，多一个斜杠是转义字符。
		String[] request_line = split[0].split("\\s");
		this.request_method = request_line[0];
		this.request_http_version = request_line[2];
		
		//对于get提取参数的方式！
		if(this.request_method.equalsIgnoreCase("get")) {
			if(request_line[1].contains("&")) {
				String[] url_split = request_line[1].split("&");
				//获取url路径
				this.request_url = url_split[0];
				
				//获取name=zhangsan,password=123的格式
				if(url_split[1].contains("?")) {
					String[] para_key_value = url_split[1].split("?");
					for(int i=0;i<para_key_value.length;i++) {
						String[] key_value = para_key_value[i].split("=");
						//将键值对给他放入到map中
						paramter.put(key_value[0], key_value[1]);
					}
				}else {
					String[] key_value = url_split[1].split("=");
					//将键值对给他放入到map中
					paramter.put(key_value[0], key_value[1]);
				}
				
			}
			//如果没有&说明，get上面没有携带参数
			this.request_url =request_line[1];
		}
		
		//对于post方法提取的方式
		//if(this.request_method.equalsIgnoreCase("post")) {
		//	
		//}
		
		//将请求头放到map当中，先用map来存储。
		for(int i=1;i<split.length;i++) {
			String[] header_key_value = split[i].split(":\\s");
			request_head.put(header_key_value[0], header_key_value[1]);
		}
		
		
		
		System.out.println();
	}
	
	
	public String getInputStream() {
		return this.paramter.toString();
	}
	
	//getHeaders方法
	public void getHeaders(String headers) {
		
	}
	//getHeader方法
	
	//setHeader方法
	
}
